<template>
	<view class="container">
		<u--form labelPosition="left" :model="LoginUser" :rules="rules" ref="uForm">
			<u-form-item label="昵称" prop="nickname" borderBottom ref="item1">
				<u--input v-model="LoginUser.nickname" border="none"></u--input>
			</u-form-item>
<!-- 			<u-form-item label="邮箱" prop="email" borderBottom ref="item1">
				<u--input v-model="LoginUser.email" border="none"></u--input>
			</u-form-item> -->
			<u-form-item label="密码" prop="password" borderBottom ref="item1">
				<u--input v-model="LoginUser.password" border="none" password></u--input>
			</u-form-item>
			<u-form-item label="头像" borderBottom ref="item1">
				<u-upload :fileList="avatar" @afterRead="afterRead" @delete="deletePic" name="avatar"
					:maxCount="1"></u-upload>
			</u-form-item>
		</u--form>
		<u-button @click="submit" type="success">提交</u-button>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				LoginUser: {
					nickname: '',
					email: '',
					password: ''
				},
				rules: {
					'nickname': {
						type: 'string',
						required: true,
						message: '请填写昵称',
						trigger: ['blur', 'change']
					},
					'email': {
						type: 'email',
						message: '请填写正确的邮箱',
						trigger: ['blur', 'change']
					},
					'password': {
						type: 'string',
						message: '密码长度必须在6-30位之间，不能包含空格',
						min: 6,
						max: 30,
						trigger: ['blur', 'change']
					}
				},
				avatar: []
			}
		},
		onReady() {
			//校验用户是否登录
			this.$u.auth.check()
			
			//onReady 为uni-app支持的生命周期之一
			this.$refs.uForm.setRules(this.rules)
			this.LoginUser = uni.getStorageSync('LoginUser') ? uni.getStorageSync('LoginUser') : {};
			if (this.LoginUser.avatar) {
				this.avatar.push({
					url: this.LoginUser.avatar
				})
			}
		},
		methods: {
			// 图片上传
			afterRead(event) {
				this.avatar.push({
					url: event.file.thumb
				})
			},
			// 删除图片
			deletePic() {
				this.avatar = []
			},
			submit() {
				this.$refs.uForm.validate().then(async (res) => {
					let data = {}
					// 判断图片是否有上传
					if (this.avatar.length > 0 && this.avatar[0].url != this.LoginUser.avatar) {
						data = {
							name: 'avatar',
							filePath: this.avatar[0].url,
							formData: {
								adminid: this.LoginUser.id,
								nickname: this.LoginUser.nickname,
								email: this.LoginUser.email,
								password: this.LoginUser.password,
								password: this.LoginUser.password ? this.LoginUser.password : '',
							}
						}
					} else {
						data = {
							adminid: this.LoginUser.id,
							nickname: this.LoginUser.nickname,
							email: this.LoginUser.email,
							password: this.LoginUser.password ? this.LoginUser.password : '',

						}
					}

					console.log(this.LoginUser.password)

					let result = await this.$u.api.admin.profile(data)
					if (result.code == 1) {
						uni.setStorageSync('LoginUser', result.data);
						this.$refs.uToast.show({
							type: 'success',
							message: result.msg,
							complete: () => {
								uni.switchTab({
									url: '/pages/admin/index'
								})
							}
						})
					} else {
						this.$refs.uToast.show({
							type: 'error',
							message: result.msg,
						})
					}
				}).catch(errors => {})
			}
		}
	}
</script>

<style>
	.container {
		height: 100%;
		margin: 0 40rpx;
		box-sizing: border-box;
	}
</style>